package com.example.publisher.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @project: kaoshi
 * @description:
 * @author: cjq
 * @create: 2022-11-06 19:57
 **/
@Configuration
public class RabbitTemplateRaliable {

    @Bean(name = "RabbitTemplateRaliable")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("相关数据：" + correlationData);
                System.out.println("确认情况：" + b);
                System.out.println("原因：" + s);
            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String text, String exchange, String routingKey) {
                System.out.println("消息：" + message);
                System.out.println("回应码：" + i);
                System.out.println("回应消息：" + text);
                System.out.println("交换机：" + exchange);
                System.out.println("路由链：" + routingKey);
            }
        });
        return rabbitTemplate;
    }
}
